import React from 'react'
import {Switch,Redirect,Route} from 'react-router-dom'
import {RouterItem} from './router-config'
interface Routes{
    routes:RouterItem[]
}

function RouterView({routes}:Routes){
    let routeArr = routes.filter((item:RouterItem) => item.component);

    let redirectArr = routes.filter((item:RouterItem) => item.redirect);

    return <Switch>
        {
            routeArr.length && routeArr.map((item:RouterItem) => <Route key={item.path} path={item.path} render={(props) => {
                return <item.component routes={item.children} {...props}></item.component>
            }
        }></Route>)}
        {
            redirectArr.length && redirectArr.map((item:RouterItem) => <Redirect key={item.path} from={item.path} to={item.redirect as string}></Redirect>)
        }
    </Switch>
}

export default RouterView